package com.mooc.http;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Covl.
 * @version 2021.1.3
 * @date 2024/3/25 17:53
 */
public class HttpRequest {
    private String httpMsg;//http协议内容
    private String requestLine;//请求行
    private String requestMethod;//请求方式
    private String requestUrl;//请求路径
    private Map<String, String> requestHeader = new HashMap<>();//请求头
    private String requestBody;//请求体
    private Map<String, String> requestParams = new HashMap<>();//请求参数

    public HttpRequest(String httpMsg) {
        this.httpMsg = httpMsg;
        //Http协议解析
        String[] rows = httpMsg.split("\r\n");
        requestLine = rows[0];
        String[] cols = requestLine.split(" ");
        requestMethod = cols[0];
        requestUrl = cols[1];
        //判断是get还是post请求
        if ("get".equalsIgnoreCase(requestMethod)) {
            //解析请求路径和请求参数
            if (requestUrl.contains("?")) {//如果请求路径中包含?，说明路径中携带请求参数
                String[] temp = requestUrl.split("\\?");
                requestUrl = temp[0];
                String s = null;
                try {
                    s = URLDecoder.decode(temp[1], "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                parseRequestParams(s);
            }
            //解析请求头
            for (int i = 1; i < rows.length; i++) {
                String[] temp = rows[i].split(": ");
                requestHeader.put(temp[0], temp[1]);
            }
        } else if ("post".equalsIgnoreCase(requestMethod)) {
            //解析请求头
            for (int i = 1; i < rows.length - 2; i++) {
                String[] temp = rows[i].split(": ");
                requestHeader.put(temp[0], temp[1]);
            }
            //保存请求体
            try {
                requestBody = URLDecoder.decode(rows[rows.length - 1], "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //如果请求体类型存在表单数据类型，则继续解析请求参数
            if (requestHeader.get("Content-Type").contains("application/x-www-form-urlencoded")) {
                parseRequestParams(requestBody);
            }
        }

    }

    public void parseRequestParams(String s) {
        String[] params = s.split("&");
        for (String param : params) {
            String[] temp = param.split("=");
            requestParams.put(temp[0], temp[1]);
        }
    }

    public String getRequestLine() {
        return requestLine;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public Map<String, String> getRequestHeader() {
        return requestHeader;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public Map<String, String> getRequestParams() {
        return requestParams;
    }
}
